package com.sc.sc250601.cook.controller;

import com.sc.sc250601.cook.pojo.Favorite;
import com.sc.sc250601.cook.pojo.FavoriteResultDTO;
import com.sc.sc250601.cook.pojo.Result;
import com.sc.sc250601.cook.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 添加收藏
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Favorite f){
        if (f.getUserId() == null) {
            return new Result(0, "请先登录");
        }
        if (f.getFavoritedId() == null || f.getFavoritedType() == null) {
            return new Result(0, "参数错误");
        }
        try {
            FavoriteResultDTO result = favoriteService.add(f);
            // code=1表示操作成功, data里告诉前端是收藏还是取消收藏
            return new Result(1, result.getMessage(), result);
        } catch (Exception e) {
            return new Result(0, "操作失败");
        }
    }

    @RequestMapping("/getFavoriteList")
    public Result getFavoriteList(Long userId){
        return new Result(1, "获取收藏列表成功", favoriteService.getFavoriteList(userId));
    }
}
